package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.admission.model.Attachment;
import com.admission.service.AttachmentManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;
@Namespace("/admin")
public class AttachmentAdminAction extends CrudActionSupport<Attachment>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private Attachment entity;
	private File picture;
	private String pictureFileName;
	private Page<Attachment> page = new Page<Attachment>(PageUtils.DEFAULT_PAGE_SIZE);
    private AttachmentManager attachmentManager;
    private String ids;
	private List<Attachment> attachmentList;

	public Attachment getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		page=attachmentManager.getPage(page);
		attachmentList=page.getResult();
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	
	/**
	 *上传文件
	 */
	@Override
	public String save() throws Exception {
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		File dir = new File(serverrealPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
		String subPath = null;
		try{
			if (picture != null) {
				subPath = new String();
				//subPath = attachmentManager.genNewFileName(pictureFileName);
				subPath= NewFileName.genNewFileName(pictureFileName, 0);
				FileUtils.copyFile(picture, new File(serverrealPath,subPath));
			}
			entity.setName(pictureFileName.substring(0, pictureFileName.lastIndexOf(".")));
		  attachmentManager.save(entity,subPath);
		 // this.addActionMessage("操作成功");
		} catch (Exception e) {
		   this.addActionMessage("操作失败，请重新操作");
	    }
		return list();
	}

	@Override
	public String delete() throws Exception {
		try{
			attachmentManager.deleteById(ids);
			 //this.addActionMessage("操作成功");
		   } catch (Exception e) {
			   this.addActionMessage("操作失败，请重新操作");
		   }  
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity = new Attachment();
		}else{
			entity = attachmentManager.getAttachmentById(id);
		}
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Attachment getEntity() {
		return entity;
	}

	public void setEntity(Attachment entity) {
		this.entity = entity;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public Page<Attachment> getPage() {
		return page;
	}

	public void setPage(Page<Attachment> page) {
		this.page = page;
	}

	public AttachmentManager getAttachmentManager() {
		return attachmentManager;
	}
	@Autowired
	public void setAttachmentManager(AttachmentManager attachmentManager) {
		this.attachmentManager = attachmentManager;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<Attachment> getAttachmentList() {
		return attachmentList;
	}

	public void setAttachmentList(List<Attachment> attachmentList) {
		this.attachmentList = attachmentList;
	}

}
